/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.anan.AAChartCore.expandablelist;

import java.util.ArrayList;

/**
 * ExpandableListContainerPosition
 *
 * @author author
 * @version 1.0
 */
public class ExpandableListContainerPosition {
    private static final int MAX_POOL_SIZE = 5;
    private static ArrayList<ExpandableListContainerPosition> sPool =
            new ArrayList<ExpandableListContainerPosition>(MAX_POOL_SIZE);
    public final static int CHILD = 1;

    public final static int GROUP = 2;

    public int groupPos;

    public int childPos;

    int flatListPos;

    public int type;

    private void resetState() {
        groupPos = 0;
        childPos = 0;
        flatListPos = 0;
        type = 0;
    }

    private ExpandableListContainerPosition() {
    }

    long getPackedPosition() {
        if (type == CHILD) return ExpandableListContainer.getPackedPositionForChild(groupPos, childPos);
        else return ExpandableListContainer.getPackedPositionForGroup(groupPos);
    }

    static ExpandableListContainerPosition obtainGroupPosition(int groupPosition) {
        return obtain(GROUP, groupPosition, 0, 0);
    }

    static ExpandableListContainerPosition obtainChildPosition(int groupPosition, int childPosition) {
        return obtain(CHILD, groupPosition, childPosition, 0);
    }

    static ExpandableListContainerPosition obtainPosition(long packedPosition) {
        if (packedPosition == ExpandableListContainer.PACKED_POSITION_VALUE_NULL) {
            return null;
        }

        ExpandableListContainerPosition elp = getRecycledOrCreate();
        elp.groupPos = ExpandableListContainer.getPackedPositionGroup(packedPosition);
        if (ExpandableListContainer.getPackedPositionType(packedPosition) ==
                ExpandableListContainer.PACKED_POSITION_TYPE_CHILD) {
            elp.type = CHILD;
            elp.childPos = ExpandableListContainer.getPackedPositionChild(packedPosition);
        } else {
            elp.type = GROUP;
        }
        return elp;
    }

    static ExpandableListContainerPosition obtain(int type, int groupPos, int childPos, int flatListPos) {
        ExpandableListContainerPosition elp = getRecycledOrCreate();
        elp.type = type;
        elp.groupPos = groupPos;
        elp.childPos = childPos;
        elp.flatListPos = flatListPos;
        return elp;
    }

    private static ExpandableListContainerPosition getRecycledOrCreate() {
        ExpandableListContainerPosition elp;
        synchronized (sPool) {
            if (sPool.size() > 0) {
                elp = sPool.remove(0);
            } else {
                return new ExpandableListContainerPosition();
            }
        }
        elp.resetState();
        return elp;
    }

    /**
     * recycle
     */
    public void recycle() {
        synchronized (sPool) {
            if (sPool.size() < MAX_POOL_SIZE) {
                sPool.add(this);
            }
        }
    }
}
